<?php
/**
* View all news, from this real estate module, also in all included module
* This action support minimal permission 
* @author: trungdt
* @since: June 7, 2011
* For HuyBDS project
*/
    global $db, $user;
    $data=array(); // array to save all news in modules
    $date_translate = exponent_lang_loadFile('subsystems/forms/controls/yuicalendarcontrol.php');
    
    /**
    * This function process all news by location_data input
    * each location_data, we get 4 news 
    * 
    * location_data now must be serialize
    * 
    * @param mixed $location_data
    * @author trungdt
    */
    function rsnews_byCat_processNews($location_data, &$data)
    {
        global $db, $loc, $date_translate ;
        $loc=unserialize($location_data);
        
        $sql="SELECT * FROM {$db->prefix}rsnewsitem WHERE (location_data='{$location_data}')  AND (publish = 0 or publish <= " . time() . ") AND (unpublish = 0 or unpublish > " . time() . ") AND (approved > 0) ORDER BY posted DESC LIMIT 0,4";
        
        // get all items
        $items=$db->selectObjectsBySql($sql);
        
        if (count($items) > 0)
        {
            $first_flag=true;
            foreach ($items as $item)
            {
                // items process
                $item->real_posted =($item->publish != 0 ? $item->publish : $item->posted);
                $nloc = exponent_core_makeLocation($loc->mod,$loc->src,$item->id);
                $item->permissions = array(
                'edit_item'=>((exponent_permissions_check('edit_item',$loc) || exponent_permissions_check('edit_item',$nloc)) ? 1 : 0),
                'delete_item'=>((exponent_permissions_check('delete_item',$loc) || exponent_permissions_check('delete_item',$nloc)) ? 1 : 0),
                'administrate'=>((exponent_permissions_check('administrate',$loc) || exponent_permissions_check('administrate',$nloc)) ? 1 : 0)
                );

                // date time
                $date_time=0;
                if ($item->edited == 0)
                    $date_time=$item->real_posted;
                else
                    $date_time=$item->edited;
                
                $item->datetime=_ab_dateFormat($date_time,!$first_flag);
                if ($first_flag)
                {
                    $first_flag=false;
                }
                // end of date time

                // for poster name
                $temp=$db->selectObject("user","id = " . $item->poster);
                $item->poster_name = "";
                if ($temp)
                {
                    $item->poster_name = $temp-> firstname ." ". $temp->lastname;
                }                                       

                // for image
                //Get the image file if there is one.
                if (isset($item->file_id) && $item->file_id > 0) {
                    $file = $db->selectObject('file', 'id='.$item->file_id);
                    $item->image_path = BASE_URL . "/".$file->directory.'/'.$file->filename;
                }
                else
                {
                    $item->image_path = "";
                }
                //Get the image file if there is one.
                if (isset($item->background_id) && $item->background_id > 0) {
                    $file = $db->selectObject('file', 'id='.$item->background_id);
                    $item->background_path ="/".$file->directory.'/'.$file->filename;
                }
                else $item->background_path="";
            }
            
            // get module title
            $title="";
            $temp=$db->selectObject("container","internal = '{$location_data}'");
            if ($temp)
            {
                $title=$temp->title;
            }
            // add to data
            $location_data=unserialize($location_data);
            $data[$location_data->src]=null; // new object
            
            // default value of first item
            $first_item=-1;
            if (count($items) > 0)
            {
                $first_item = array_shift($items);
            }
            $data[$location_data->src]->first_item=$first_item;
            $data[$location_data->src]->items=$items;
            $data[$location_data->src]->title=$title;
        }
        
        // 
        return true; // :)
    }
    
    $config = $db->selectObject('rsnewsmodule_config',"location_data='".serialize($loc)."'");
    if (empty($config)) {
        $config->sortorder = 'DESC';
        $config->sortfield = 'posted';
        $config->item_limit = 10;
        $config->enable_pagination = 1;
        $config->aggregate = array();
    }
    
    // for current module
    rsnews_byCat_processNews(serialize($loc), $data);
    
    if (!empty($config->aggregate)) {
        $locations = unserialize($config->aggregate);
        foreach ($locations as $source) {
            $tmploc = null;
            $tmploc->mod = 'rsnewsmodule';
            $tmploc->src = $source;
            $tmploc->int = '';
            //$locsql .= " OR location_data='".serialize($tmploc)."'";
            rsnews_byCat_processNews(serialize($tmploc), $data);
        }
    }
    
    
    // 10/01/2010 - immanuel192 - get module title - begin
    // get module title
    // because if the client switch page, so, that is the action and exponent does not transfer $title and $views        
    $temp=$db->selectObject("container","internal = '" . serialize($loc) . "'");
    if ($temp)
    {
        $title=$temp->title;
    }
    $view="_real_estate_bygroup"; // default viewer for this action heehe
    
    // 10/01/2010 - immanuel192 - get module title - end
    
    $template = new template('rsnewsmodule',$view,$loc);

    $template->assign('moduletitle',$title);
    $template->register_permissions(
        array('administrate','configure','add_item','delete_item','edit_item','manage_approval','view_unpublished'),
        $loc
    );
    
    $template->assign('news',$data);
    
    $template->assign('config', $config);
    // Change if clase here, only check that flow_has_set has check or not
    if (!isset($_REQUEST['flow_has_set']))
        exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
    
    // page title    
    $page_title=exponent_lang_getText("rsnews_view_byGroup_title");
    $page_title.=" - ".SITE_TITLE;
    _ab_theme_action_register_title($page_title);
    // Feb 12, 2011 - trungdt - set page title - end
    
    $template->output();
?>